

package com.trailer.base.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.google.gson.Gson;
import com.trailer.base.R;
import com.trailer.base.base.BApp;

import static android.text.TextUtils.isEmpty;

/**
 * Sharepreference 常用的操作类
 */
public class S {
    public static final String S_USER = "user";
    public static final String MSG = "msg";
    public static final String SETTINGS = "settings";
    public static final String COL_DISABLED_IDS = "col_disabled_ids";
    public static final String COL_DISABLED_GROUP_IDS = "col_disabled_group_ids";
    public static final java.lang.String DB_LOGIN = "login";
    public static final String SMS_TIME = "sms_time";
    public static final String FIRST_PREF = "first_pref";
    public static final java.lang.String LOGIN = "login";
    public static final String FILE = "file";
    public static final java.lang.String LOCATION = "location";
    public static final java.lang.String COOKIE = "COOKIE";
    public static final java.lang.String VERSION = "VERSION";
    public static final java.lang.String INDEX_DATA = "INDEX_DATA";
    public static final String TABTWO_STORE = "TABTWO_STORE";
    public static final java.lang.String PUSH = "PUSH";
    public static final String jSON_DATA = "json_data";
    private static Gson gson;

    private static SharedPreferences prefrence;
    private static SharedPreferences customPrefrence;

    private static S s = null;
    private static S customSpUtil = null;
    private static Editor editor = null;


    public static S instance(Context applicationContext) {
        if (s == null) {
            s = new S();
            gson = new Gson();
        }

        if (prefrence == null) {
            String fileName = applicationContext.getString(R.string.app_name);
            prefrence = applicationContext.getSharedPreferences(fileName, 0);
        }
        return s;
    }


    public static S instance() {
        if (s == null) {
            s = new S();
            gson = new Gson();
        }

        if (prefrence == null) {
            prefrence = BApp.instance().getApplicationContext().getSharedPreferences("empoly", 0);
        }
        if (editor == null) {
            editor = prefrence.edit();
        }
        return s;
    }

    private static S newInstance(String fileName) {
        if (customSpUtil == null) {
            customSpUtil = new S();
            gson = new Gson();
        }

        if (customPrefrence == null) {
            customPrefrence = BApp.instance().getApplicationContext().getSharedPreferences(fileName, 0);
        }
        return customSpUtil;
    }

    public S putString(String key, String value) {
        editor.putString(key, value);
        return s;
    }

    public S ok() {
        editor.apply();
        editor = null;
        return s;
    }

    public static S putCusString(String fileName, String key, String value) {
        newInstance(fileName);
        Editor editor = customPrefrence.edit();
        editor.putString(key, value);
        editor.apply();

        return customSpUtil;
    }

    public String getString(String key) {
        return prefrence.getString(key, "");
    }

    public static String getCusString(String fileName, String key) {
        newInstance(fileName);

        return customPrefrence.getString(key, "");
    }

    public static String getString(String key, String defaultStr) {
        return prefrence.getString(key, defaultStr);
    }

    public static S putBoolean(String key, Boolean value) {
        Editor editor = prefrence.edit();
        editor.putBoolean(key, value);
        editor.apply();
        return s;
    }

    public static boolean getBoolean(String key) {
        return prefrence.getBoolean(key, false);
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        return prefrence.getBoolean(key, defaultValue);
    }

    public static boolean getBooleanWithTrue(String key) {
        return prefrence.getBoolean(key, true);
    }

    public static void putLong(String key, Long value) {
        Editor editor = prefrence.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public static long getLong(String key) {
        return prefrence.getLong(key, 0L);
    }

    public static S putInt(String key, Integer value) {
        Editor editor = prefrence.edit();
        editor.putInt(key, value);
        editor.apply();

        return s;
    }

    public static int getInt(String key) {
        return prefrence.getInt(key, 0);
    }

    public static S putFloat(String key, Float value) {
        Editor editor = prefrence.edit();
        editor.putFloat(key, value);
        editor.apply();
        return s;
    }

    public static float getFloat(String key) {
        return prefrence.getFloat(key, 0.0F);
    }


    public S put(String key, Object value) {
        if (value != null) {
            String json = gson.toJson(value);
            s.putString(key, json);
        }
        return s;
    }

    public <T> T get(String key, Class<T> clazz) {
        String string = getString(key);
        if (!isEmpty(string)) {
            return gson.fromJson(string, clazz);
        } else {
            return null;
        }
    }


    public static void remove(String key) {
        Editor editor = prefrence.edit();
        editor.remove(key);
        editor.apply();
    }


    /**
     * @param type 保存的语言类型
     * @param ac
     */
    public static void saveLastUpdateData(int type, Context ac) {
        if (ac == null) {
            return;
        }
        SharedPreferences sp = ac.getSharedPreferences("LANGUAGE", ac.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt("LaUpdate", type);
        editor.commit();
    }


    /**
     * @param ac
     *
     * @return 获得保存的语言, 默认中文2, 1是英文
     */
    public static int getLastUpdateData(Context ac) {

        int type;
        if (ac == null) {
            return 2;
        }
        try {
            SharedPreferences sp = ac.getSharedPreferences("LANGUAGE", ac.MODE_PRIVATE);
            type = sp.getInt("LaUpdate", 2);
            return type;
        } catch (Exception e) {
            type = 2;
        }
        return type;
    }

}
